package com.ruoyi.web.controller.clinic;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.clinic.dto.clinicdoctor.*;
import com.ruoyi.clinic.service.IClinicDoctorInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.validation.Valid;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 我负责的医生Controller
 *
 * @author Tommy
 * @date 2025-11-08
 */
@Controller
@RequestMapping("/clinic/myDoctor")
public class ClinicMyDoctorController extends BaseController {

    private String prefix = "clinic/myDoctor";

    @Autowired
    private IClinicDoctorInfoService clinicDoctorInfoService;

    @RequiresPermissions("clinic:myDoctor:view")
    @GetMapping()
    public String myDoctor() {
        return prefix + "/myDoctor";
    }

    /**
     * 查询医生信息列表，只查询我负责的医生
     */
    @RequiresPermissions("clinic:myDoctor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ClinicDoctorReqDTO dto) {
        startPage();
        dto.setCreateDept(this.getDeptId());
        dto.setCreateById(this.getUserId());
        SysUser sysUser = this.getSysUser();
        List<ClinicDoctorResDTO> list = clinicDoctorInfoService.queryMyDoctorPageList(dto, sysUser);
        return getDataTable(list);
    }

    /**
     * 导出医生信息列表
     */
    @RequiresPermissions("clinic:myDoctor:export")
    @Log(title = "我负责的医生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ClinicDoctorReqDTO dto) {
        dto.setCreateDept(this.getDeptId());
        dto.setCreateById(this.getUserId());
        SysUser sysUser = this.getSysUser();
        List<ClinicDoctorResDTO> list = clinicDoctorInfoService.queryMyDoctorPageList(dto, sysUser);
        ExcelUtil<ClinicDoctorResDTO> util = new ExcelUtil<>(ClinicDoctorResDTO.class);
        return util.exportExcel(list, "我负责的医生数据");
    }

    /**
     * 导入医生信息数据
     *
     * @param file          导入的文件
     * @param updateSupport 是否支持更新
     * @return 响应
     * @throws Exception 异常
     */
    @Log(title = "我负责的医生", businessType = BusinessType.IMPORT)
    @RequiresPermissions("clinic:myDoctor:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ClinicDoctorImportDTO> util = new ExcelUtil<>(ClinicDoctorImportDTO.class);
        List<ClinicDoctorImportDTO> doctorImportDTOList = util.importExcel(file.getInputStream());
        if (CollectionUtil.isNotEmpty(doctorImportDTOList)) {
            doctorImportDTOList.forEach(one -> {
                one.setCreateById(this.getUserId());
                one.setCreateBy(this.getLoginName());
                one.setUpdateById(this.getUserId());
                one.setUpdateBy(this.getLoginName());
                // 部门ID
                one.setCreateDept(this.getDeptId());
                // 业务人员ID，默认为当前登录用户
                one.setSalesmanId(this.getUserId());
                one.setCreateTime(DateUtils.getNowDate());
                one.setUpdateTime(DateUtils.getNowDate());
            });
        }
        String message = clinicDoctorInfoService.importMyDoctorData(doctorImportDTOList, updateSupport, this.getSysUser());
        return AjaxResult.success(message);
    }

    /**
     * 导入医生信息模板
     *
     * @return 导入模板
     */
    @RequiresPermissions("clinic:myDoctor:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ClinicDoctorImportDTO> util = new ExcelUtil<>(ClinicDoctorImportDTO.class);
        return util.importTemplateExcel("我负责的医生数据");
    }

    /**
     * 新增医生信息
     */
    @RequiresPermissions("clinic:myDoctor:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存医生信息
     */
    @RequiresPermissions("clinic:myDoctor:add")
    @Log(title = "我负责的医生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Valid ClinicDoctorUpdateDTO dto) {
        dto.setCreateById(this.getUserId());
        dto.setCreateBy(this.getLoginName());
        dto.setUpdateById(this.getUserId());
        dto.setUpdateBy(this.getLoginName());
        // 部门ID
        dto.setCreateDept(this.getDeptId());
        // 业务人员ID，默认为当前登录用户
        dto.setSalesmanId(this.getUserId());
        return toAjax(clinicDoctorInfoService.insertClinicDoctorInfo(dto));
    }

    /**
     * 修改医生信息
     */
    @RequiresPermissions("clinic:myDoctor:edit")
    @GetMapping("/edit/{doctorId}")
    public String edit(@PathVariable("doctorId") Long doctorId, ModelMap mmap) {
        SysUser sysUser = this.getSysUser();
        ClinicDoctorDetailDTO clinicDoctorDetailDTO = clinicDoctorInfoService.queryClinicDoctorDetail(doctorId, sysUser);
        mmap.put("clinicDoctorInfo", clinicDoctorDetailDTO);
        return prefix + "/edit";
    }

    /**
     * 修改保存医生信息
     */
    @RequiresPermissions("clinic:myDoctor:edit")
    @Log(title = "我负责的医生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Valid ClinicDoctorUpdateDTO dto) {
        dto.setUpdateById(this.getUserId());
        dto.setUpdateBy(this.getLoginName());
        return toAjax(clinicDoctorInfoService.updateClinicDoctorInfo(dto));
    }

    /**
     * 删除医生信息
     */
    @RequiresPermissions("clinic:myDoctor:remove")
    @Log(title = "我负责的医生", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(clinicDoctorInfoService.deleteClinicDoctorInfoByDoctorIds(ids));
    }
}
